﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    {
       
        public ActionResult MTWZ09(string returnUrl)
        {
            ModelState.Clear();
            if (WBBS == null)
            {
                WBREQF();//可以留位的布卷以及已经留位的布卷
            }
            ViewBag.WBBS = WBBS; 
            ViewBag.ReturnUrl = returnUrl;
            return PartialView();
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MTWZ09(List<WBBSTModel> m, string returnUrl)
        { 
            ModelState.Clear();
            return RedirectToAction(MAPP, MAPP);
        }
        public ActionResult MTWZ09F(string returnUrl)
        {
            ModelState.Clear();
            if (WBBS == null)
            {
                WBREQF();//可以留位的布卷以及已经留位的布卷
            } 
            ViewBag.WBBSF = WBBSF;
            ViewBag.ReturnUrl = returnUrl;
            return PartialView();
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MTWZ09F(List<WBBSTModel> m, string returnUrl)
        { 
            return RedirectToAction(MAPP, MAPP);
        }
        public void WBREQF()
        {
            TargetMTR = 0;
            MessageInfo msg = new MessageInfo();
            msg.LIEFSCH = MVVT.LIEFSCH;
            msg.AUFTYP = MVVT.AUFTYP;
            msg.KRS = MVVT.KRS;
            msg.LFD = MVVT.LFD;
            msg.POS = MVVT.POS;
            msg.LO = MVVT.SLO;
            msg.LB = MVVT.SLB;
            DataSet SReult = WBB.WB2WH(msg);
            if (WBBS == null)
            {
                WBBS = new List<WBBSTModel>();
            }
            foreach (DataRow row in SReult.Tables[0].Rows)
            {
                WBBSTModel m = new WBBSTModel();
                m.WBBSTKNR = row["WBBSTKNR"].ToString();
                m.WBBAUFLFD = row["WBBAUFLFD"].ToString(); 
                m.QMTR = Convert.ToDouble(row["WBBISTKLG"].ToString()) / 1000;
                m.QYD = Helper.RoundX(m.QMTR / 0.9144, 2);
                m.REMARK = "已留位";
                WBBS.Add(m);
            }

            msg.WAKZ = MVVT.WAKZ;
            msg.ART = MVVT.ART;
            msg.VAR = MVVT.VAR;
            msg.FARBE = MVVT.FARBE;
            msg.LO = MVVT.SLO;
            msg.LB = MVVT.SLB;
            SReult = WBB.WBREQ(msg);
            if (WBBSF == null)
            {
                WBBSF = new List<WBBSTModel>();
            }
            foreach (DataRow row in SReult.Tables[0].Rows)
            {
                WBBSTModel m = new WBBSTModel();
                m.WBBSTKNR = row["WBBSTKNR"].ToString();
                m.WBBAUFLFD = row["WBBAUFLFD"].ToString(); 
                m.QMTR = Convert.ToDouble(row["WBBISTKLG"].ToString()) / 1000;
                m.QYD = Helper.RoundX(m.QMTR / 0.9144, 2);
                m.REMARK = "自由";
                WBBSF.Add(m);
            } 
        }
        /// <summary>
        /// 装载所有布卷
        /// </summary>
        /// <param name="returnUrl"></param>
        /// <returns></returns>
        [ChildActionOnly]
        public ActionResult STWZ09(string returnUrl)
        {
            if (MVVT == null)
                return PartialView(); 
            MVVT.SLO = "01";
            MVVT.SLB = "CP";

            ModelState.Clear();
            /*
            if (UserID == "JLTV    ")
            {
                if (LMODEL != null)
                {
                    if (!LMODEL.IsDataValid)
                    {
                        return PartialView(LMODEL);
                    }
                }
                if (WBBS != null)
                {
                    var result = WBBS.Where(p => p.WBBAUFLFD == "000000").OrderBy(p => p.WBBSTKNR);
                    foreach (WBBSTModel mx in result)
                    {
                        mx.TERMNO = app.TERMNO;
                        mx.STAFF = app.User;
                        mx.VKPAUF = MVVT.VKPAUF;
                        mx.LIEFSCH = MVVT.LIEFSCH;
                        if (LMODEL != null)
                        {
                            mx.REMARK = ((WBBSTModel)LMODEL).WBBSTKNR + LMODEL.REMARK;
                        }
                        return PartialView(mx);
                    }
                }
                WBBSTModel m = new WBBSTModel();
                m.TERMNO = app.TERMNO;
                m.STAFF = app.User;
                m.VKPAUF = MVVT.VKPAUF;
                m.LIEFSCH = MVVT.LIEFSCH;
                m.REMARK = "请扫描"; 
                if (LMODEL != null)
                {
                    m.REMARK = ((WBBSTModel)LMODEL).WBBSTKNR + LMODEL.REMARK;
                }
                return PartialView(m);
            }
            */
            return PartialView();
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult STWZ09(WBBSTModel model, string returnUrl)
        {
            if (MVVT == null)
            {
                model.IsDataValid = false;
                model.REMARK = "仅在发货单存在条件下才能操做";
            }
            bool IsAllocate = true;
            model.IsDataValid = true;
            model.REMARK = "取消"; 
             var result = WBBS.Where(p => p.WBBSTKNR==model.WBBSTKNR).OrderBy(p => p.WBBSTKNR);
             if (result.Count() == 0)
             {
                 model.REMARK = "留位";
                 IsAllocate = false;
             } 
            if (model.IsDataValid)
            {
                if (!IsAllocate)
                {
                    WBBS61(model);//留位
                }
                else
                {
                    WBBS62(model);//取消留位
                }
            }
            LMODEL = model;
            ModelState.Clear();
            return RedirectToAction(MAPP, MAPP);
        }
        public void WBBS61(WBBSTModel model)
        {
            MessageInfo msg = new MessageInfo();
            msg.Sender = RUNID;
            msg.AUFTYP = MVVT.AUFTYP;
            msg.KRS = MVVT.KRS;
            msg.LFD = MVVT.LFD;
            msg.POS = MVVT.POS;
            msg.STKNR = model.WBBSTKNR;
            msg.LIEFSCH = MVVT.LIEFSCH;
             WBB.WBB61(msg); 
            model.WBBAUFLFD = MVVT.LFD; 
            Allocate(model);
        }
        public void WBBS62(WBBSTModel model)
        {

            MessageInfo msg = new MessageInfo();
            msg.Sender = RUNID;
            msg.AUFTYP = MVVT.AUFTYP;
            msg.KRS = MVVT.KRS;
            msg.LFD = MVVT.LFD;
            msg.POS = MVVT.POS;
            msg.STKNR = model.WBBSTKNR;
            msg.LIEFSCH = MVVT.LIEFSCH;
            WBB.WBB62(msg);
            Free(model);
        }
        private void Allocate(WBBSTModel model)
        {
            WBBSTModel mx = null;
            var result = WBBSF.Where(p => p.WBBSTKNR == model.WBBSTKNR).OrderBy(p => p.WBBSTKNR);
            foreach (WBBSTModel m in result)
            {
                mx = m;
            }
            if (mx != null)
            {
                mx.WBBAUFLFD = model.WBBAUFLFD;
                WBBS.Insert(0, mx);
                WBBSF.Remove(mx);
            }
        }
        private void Free(WBBSTModel model)
        {
            WBBSTModel mx = null;
            var result = WBBS.Where(p => p.WBBSTKNR == model.WBBSTKNR).OrderBy(p => p.WBBSTKNR);
            foreach (WBBSTModel m in result)
            {
                mx = m;
            }
            if (mx != null)
            {
                mx.WBBAUFLFD = "000000";
                WBBSF.Insert(0, mx);
                WBBS.Remove(mx);
            }
        }
    }
}
